package com.qfoud.edu.aunt.commons.service.impl;

import com.qfoud.edu.aunt.commons.dao.IMenuDAO;
import com.qfoud.edu.aunt.commons.dao.IRoleDAO;
import com.qfoud.edu.aunt.commons.dao.IUserDAO;
import com.qfoud.edu.aunt.commons.dao.impl.MenuDAO;
import com.qfoud.edu.aunt.commons.dao.impl.RoleDAO;
import com.qfoud.edu.aunt.commons.dao.impl.UserDAO;
import com.qfoud.edu.aunt.commons.enum1.ResponseCode;
import com.qfoud.edu.aunt.commons.exception.BusinessException;
import com.qfoud.edu.aunt.commons.pojo.Menu;
import com.qfoud.edu.aunt.commons.pojo.Role;
import com.qfoud.edu.aunt.commons.pojo.User;
import com.qfoud.edu.framwork.utils.StringUtils;
import com.qfoud.edu.shiro.framwork.auth.AuthticationInfo;
import com.qfoud.edu.shiro.framwork.pojo.Subject;
import com.qfoud.edu.shiro.framwork.service.base.UserDetailService;

import java.util.List;

/**
 * @author xiaobobo
 * @title: MyUserDetailService
 * @projectName cd-java-2303-project-parent
 * @description: TODO
 * @date 2023/5/11  15:40
 */
@com.qfoud.edu.shiro.framwork.annotation.UserDetailService
public class MyUserDetailService extends UserDetailService {

    private IUserDAO userDAO=new UserDAO();

    private IRoleDAO roleDAO=new RoleDAO();

    private IMenuDAO menuDAO=new MenuDAO();

    @Override
    public Subject loadUserByName(String username) throws Exception {
        //通过用户名查询用户数据
        if(StringUtils.isEmpty(username)){
            throw new BusinessException(ResponseCode.USERNAME_ERROR.getCode(),
                    ResponseCode.PARAMETER_ERROR.getMessage());
        }
        //执行到这里说明有用户名
        User user=null;
        try {
            user=userDAO.findUserByName(username);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(null==user){
            throw new BusinessException(ResponseCode.USERNAME_ERROR.getCode(),
                    ResponseCode.USERNAME_ERROR.getMessage());
        }
        //TODO 正常开发这里还需要查询 (用户角色 用户权限)
        //通过用户id查询用户具有的角色
        List<Role> roleList= roleDAO.findRolesByUserId(user.getId());
        //通过用户id查询用户具有的权限
        List<Menu> menuList=menuDAO.findPermsByUserId(user.getId());

        //最终封装成我们想要的对象进行返回
        Subject subject = new Subject();
        //将角色和权限转换成String类型
        initStringRoleAndMenu(subject,roleList,menuList);
        //这里还需要进行对象的初始化
        AuthticationInfo authticationInfo = subject.getAuthticationInfo();
        authticationInfo.setUsername(user.getUsername());
        authticationInfo.setPassword(user.getPassword());

        return subject;
    }

    /**
     * 将觉角色和权限放到subject中去
     * @param subject
     * @param roleList
     * @param menuList
     */
    private void initStringRoleAndMenu(Subject subject,List<Role> roleList, List<Menu> menuList) {
        for (int i = 0; i <roleList.size() ; i++) {
            subject.getRoles().add(roleList.get(i).getRoleName());
        }
        //接下来初始化权限
        for (int i = 0; i <menuList.size() ; i++) {
            subject.getPerms().add(menuList.get(i).getPermVal());
        }
    }
}
